package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.CompanyMapper;
import org.example.mapper.JobMapper;
import org.example.pojo.Company;
import org.example.pojo.Job;
import org.example.pojo.JobDto;
import org.example.pojo.PageBean;
import org.example.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public PageBean<Job> list(Integer pageNum, Integer pageSize) {
        //1.创建一个PageBean对象
        PageBean<Job> pb = new PageBean<>();

        //2.开启分页插件功能
        PageHelper.startPage(pageNum, pageSize);

        //将数据转为Page类型
        try{
            List<Job> Jobs =  jobMapper.selectAllJobs();
            Page<Job> page = (Page<Job>) Jobs;

            //把数据填充到PageBean对象中
            pb.setTotal(page.getTotal());
            pb.setItem(page.getResult());
            return pb;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public Job findJobById(Integer jobId) {
        return jobMapper.findJobById(jobId);
    }

    @Override
    public JobDto getJobById(Integer jobId) {
        //查找岗位信息
        Job job = jobMapper.findJobById(jobId);
        //查找公司信息
        Company company = companyMapper.findCompanyById(job.getCompanyId());

        JobDto jobDto = new JobDto();
        jobDto.setJob(job);
        jobDto.setCompany(company);

        return jobDto;
    }

    @Override
    public List<Job> analysisJobs(String tags, String degree, String low_experience, String up_experience, String location) {
        List<Job> jobs=jobMapper.analysisJobs(tags, degree, low_experience,up_experience,location);
        return jobs;
    }

    @Override
    public List<Job> analysisRole(String tags, String location) {
        List<Job> jobs=jobMapper.analysisRole(tags,location);
        return jobs;
    }

    @Override
    public Job selectByJobId(Integer myFavorId, Integer companyId) {
        return jobMapper.selectByJobId(myFavorId, companyId);
    }

    @Override
    public void deleteJob(Integer jobId, Integer status) {
        jobMapper.deleteJob(jobId, status);
    }

    @Override
    public List<Job> getCompanyJobList(Integer companyId) {
        return jobMapper.getCompanyJobList(companyId);
    }

    @Override
    public void updateCompany(Company company) {
        jobMapper.updateCompany(company);
    }

    @Override
    public Integer getCount(List<Integer> counts) {
        return jobMapper.getCount(counts);
    }

    @Override
    public List<Job> getHotJobList() {
        return jobMapper.getHotJobList();
    }

    @Override
    public void insertJob(Job job) {
        jobMapper.insertJob(job);
    }


}
